//
//  AppDelegate.m
//  WeiboOC
//
//  Created by yang on 15/11/11.
//  Copyright © 2015年 yang. All rights reserved.
//

#import "AppDelegate.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self setTitle];
    [self registerNotification];
    self.window.rootViewController = [self defaultRootController];
    [self.window makeKeyAndVisible];
    return YES;
}
//监听通知,切换控制器
- (void)registerNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchToRootViewControler:) name:AppSwitchRootViewControler object:nil];
}
//监听方法
- (void)switchToRootViewControler:(NSNotification *)notify{
    if (notify.object != nil ) {
        self.window.rootViewController = [[YQWelComeViewController alloc]init];
    }else{
        
     self.window.rootViewController = [[MainViewController alloc]init];
    }
 
}
//移除通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
// 判断用户是否登陆 并且是否是更新版本
- (UIViewController *)defaultRootController{
    YQUserAccountViewModel *user = [[YQUserAccountViewModel alloc]init];
    MainViewController *main = [[MainViewController alloc]init];
    if (user.userLogin) {
        YQNewFeatureViewController *newFeature = [[YQNewFeatureViewController alloc]init];
        YQWelComeViewController *welcome = [[YQWelComeViewController alloc]init];
        return [self isNewVersion] ? newFeature : welcome;
    }
    return main;

}
//判断是不是新版本
- (BOOL)isNewVersion
{
    //获取当前版本号
    NSString* versionNow = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    double versionNum = [versionNow doubleValue];
    NSString* key = @"version";
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    //取上一次的版本号
    double versionLast = [userDefaults doubleForKey:key];
    //把当前版本号存入沙盒
    [userDefaults setDouble:versionNum forKey:key];
    [userDefaults synchronize];
    //比较两次的版本号
    return versionNum > versionLast;
}
//设置字体颜色
- (void)setTitle
{
    [UITabBar appearance].tintColor = titleColor;
    [UINavigationBar appearance].tintColor = titleColor;
}
@end
